import Vue from 'vue'
import Vuex from 'vuex'
// const plug = uni.requireNativePlugin('UZK-MeiQiaSdk');
// #ifdef APP-PLUS
const zhiChiSobot = uni.requireNativePlugin('zhiChi-SobotKit');
// #endif

Vue.use(Vuex)
import globalUtil from '../common/globalUtil.js'

const store = new Vuex.Store({
	state: {
		hasLogin: false,
		jsApiAppId: "wx9002f3ee0092e476",
		// downUrl: "https://a.app.qq.com/o/simple.jsp?pkgname=com.app.fnwqsg",
		downUrl: "", // 下载app地址
		imgSite: "https://img.lianshanggogo.com/", // 普通商品突破前缀
		sourceImgSite: 'http://47.104.20.233:8080/ipfs/', // 溯源图片前缀
		// apiSite: "http://49.4.31.246:18000/",
		apiSite: "https://api.lianshanggogo.com/",
		url: 'https://api.lianshanggogo.com/', // 分享地址
		windowTop: '0',
		message: null,
		msgNumber: 0, // 客服消息数量
		android_video_code: '247789742261796864', // 广告联盟 视频code
		ios_video_code: '247789935631794176',// 广告联盟 视频code
		android_advertising_code: '250045791148703744',// 广告联盟 广告code
		ios_advertising_code: '250045620130152448',// 广告联盟 广告code
		android_read_code: '250048492918013952',// 广告联盟 阅读code
		ios_read_code: '250048628440170496',// 广告联盟 阅读code
		android_coopen_code: '252250435065217024', // 广告联盟 开屏code
		ios_coopen_code: '252250322305548288', // 广告联盟 开屏code
	},
	mutations: {
		login(state) {
			state.hasLogin = true;

			// #ifdef APP-PLUS
			var params = {};
			const tokenInfo = uni.getStorageSync('tokenInfo');
			if (tokenInfo) {
				params['header'] = {
					"Access-Control-Allow-Origin": true,
					"ecp-vt": JSON.parse(tokenInfo)["token"],
					"ecp-vu": JSON.parse(tokenInfo)["customerId"]
				};

				var pinf = plus.push.getClientInfo();
				console.log("pinf = " + pinf);
				var cid = pinf.clientid; //客户端标识
				console.log("客户端标识 = " + cid);

				var gtType = "";
				if (plus.os.name.toLowerCase() === 'android') {
					gtType = "1";
				} else {
					gtType = "2";
				}
				uni.request({
					url: state.apiSite + '/CustomerApi/bindUniPush',
					method: 'POST',
					header: params['header'] || {},
					data: {
						gtType: gtType, //1.安卓 2.IOS
						gtToken: cid
					},
					success: function(res) {
						if (res.data.success) {

							plus.push.addEventListener('click', function(msg) {
								uni.navigateTo({
									url: "/pageUser/msgCenter"
								});
							}, false);
						}
					},
				});

				// #ifdef APP-PLUS
				//消息角标
				if (plus.os.name.toLowerCase() == 'android') {} else {

					uni.request({
						url: state.apiSite + '/customerNoticeApi/customerNoticeUnreadCountApi',
						method: 'POST',
						header: params['header'] || {},
						data: {
							cnType: "",
							cnStatus: '0'
						},
						success: function(res) {
							if (res.data.success) {
								console.log(res.data.response);
								plus.runtime.setBadgeNumber(res.data.response);
							}
						},
					});
				}
				// #endif
			}
			// #endif
		},
		logout(state) {
			state.hasLogin = false;
			uni.removeStorage({
				key: 'tokenInfo'
			});

			uni.navigateTo({
				url: '/pages/login/userLogin'
			});
		},
		bindBack(state) {
			state.hasLogin = false;
			uni.removeStorage({
				key: 'tokenInfo'
			});
		},
		getWindowTop() {
			uni.getSystemInfo({
				success(res) {
					console.log('res==>', res);
					state.windowTop = res.windowTop + 'px';
				}
			});
		}
	},

	actions: {
		// 开启客服
		openService(store) {
			globalUtil.getUserInfo().then(user => {
				zhiChiSobot.startZhiChi({
					app_key: globalUtil.serviceKey,
					partnerid: user.data.response.id,
					user_nick: user.data.response.nickName,
				}, result => {
					store.state.message = result.value || ''
					store.state.msgNumber = result.num || 0
					uni.setStorageSync('service', result)
				});
			});
		},
		// 获取消息数量
		getUnReadMessage() {
			globalUtil.getUserInfo().then(user => {
				zhiChiSobot.getUnReadMessage({
					userId: user.data.response.id,
					partnerid: user.data.response.id
				}, result => {
					console.log("getUnReadMessage", result)
					store.state.msgNumber = result.value - 0 || 0
				});
			});
		}
	},
})

export default store
